// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// R [author] rhyme_lph
// s [email]  rhymelph@gmail.com
// N [github] https://github.com/rhymelph

import 'dart:io';

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/widgets/bottom_sheet/default_bottom_sheet.dart';
import 'package:finance_app/src/widgets/button/ace_back_button.dart';
import 'package:finance_app/src/widgets/button/normal_button.dart';
import 'package:finance_app/src/widgets/picture/crop_border.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/camera/edit_photo_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:finance_app/generated/a.dart';
import 'package:cropper/cropper.dart';

@RouterMeta(paramName: "editPhotoPage")
class EditPhotoPage extends StatefulWidget {
  final String filepath;
  final bool isFromPhoto;

  const EditPhotoPage({Key key, this.filepath, this.isFromPhoto})
      : super(key: key);

  @override
  _EditPhotoPageState createState() => _EditPhotoPageState();
}

class _EditPhotoPageState extends State<EditPhotoPage>
    with PagePresenter<EditPhotoPage, EditPhotoProvider> {
  @override
  void initState() {
    super.initState();
    provider.initData(widget.filepath, widget.isFromPhoto);
  }

  @override
  Widget get buildWidget => Scaffold(
        backgroundColor: Colors.black,
        body: Column(
          children: [
            Padding(
              padding: EdgeInsets.only(
                top: MediaQuery.of(context).padding.top,
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  AceBackButton(),
                  Text(
                    S.current.edit_photo,
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: getSp(18),
                      fontWeight: FontWeight.w700,
                      height: 36 / 18,
                    ),
                    textAlign: TextAlign.center,
                  ),
                  PressedOpacityButton(
                    onPressed: provider.onRotate,
                    padding: EdgeInsets.all(getDp(18)),
                    child: Image.asset(
                      A.assetsImagesStyleRefresh_1,
                      width: getDp(23),
                      height: getDp(23),
                      fit: BoxFit.contain,
                    ),
                  )
                ],
              ),
            ),
            Expanded(
              child: Center(
                child: Crop(
                  dimColor: Colors.transparent,
                  controller: provider.cropController,
                  child: Image.file(File(widget.filepath)),
                  helper: CropBorder(),
                ),
              ),
            ),
            SizedBox(
              height: getDp(6),
            ),
            DefaultBottomSheet(
              hasClose: false,
              topMargin: 24,
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  SizedBox(
                    height: getDp(18),
                  ),
                  NormalButton(
                    onTap: provider.onDone,
                    title: S.current.done,
                  ),
                  AppSpaces.h15,
                  NormalButton(
                    isBorder: true,
                    onTap: provider.onRetakePhotoOrSelect,
                    title: widget.isFromPhoto == true
                        ? S.current.choose_again
                        : S.current.retake_photo,
                  ),
                  SizedBox(
                    height: MediaQuery.of(context).padding.bottom + getDp(24),
                  )
                ],
              ),
            ),
          ],
        ),
      );
}
